<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Bloc
{
    public function getBlocActive($emplacement)
    {
        $bloc_content = '';

        $q = $GLOBALS['db']->prepare("SELECT * FROM ". PREFIX ."bloc WHERE emplacement = :emplacement ORDER BY ordre");
        $q->bindParam('emplacement', $emplacement, PDO::PARAM_INT);
        $q->execute();
        $blocs = $q->fetchAll();
        $q->closeCursor();

        foreach($blocs as $bloc):
            if($bloc['contenu'] != ''):
               $bloc_content .= Bloc::getHTMLBloc($bloc['id'], $bloc['name'], $bloc['contenu']);
               $bloc_content .= '<br /><br />';
            else:
                if( is_file(IMMOPHP_ROOT_PATH . $bloc['fichier'])):
                    require_once IMMOPHP_ROOT_PATH . $bloc['fichier'];

                    if(is_callable($bloc['fonction'])):
                        $bloc_content .= $bloc['fonction']();
                        $bloc_content .= '<br /><br />';
                    endif;

                endif;
            endif;
        endforeach;

        return $bloc_content;
    }

    private function getPaserContenu($contenu)
    {
        $contenu = stripslashes($contenu);
        $contenu = str_replace('<p>','<div><br />',$contenu);
        $contenu = str_replace('<p style=','<div style=',$contenu);
        $contenu = str_replace('</p>','<br /></div>',$contenu);

        return $contenu;
    }

    private function getHTMLBloc($id, $titre, $contenu)
    {
        $GLOBALS['smarty']->caching = 1;

        if( !$GLOBALS['smarty']->isCached('bloc_html.tpl', 'bloc_id'.$id) ):

            $GLOBALS['smarty']->assign( array('bloc_id' => $id, 'bloc_titre' => $titre, 'bloc_contenu' => Bloc::getPaserContenu($contenu) ) );

        endif;

        $b = $GLOBALS['smarty']->fetch('bloc_html.tpl', 'bloc_id'.$id);

        $GLOBALS['smarty']->caching = 0;

        return $b; 
    }
}

?>
